package com.slipper.system.backup.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.slipper.system.affix.service.AffixItemService;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;

import com.slipper.system.backup.dao.BackupDao;
import com.slipper.system.backup.bo.BackupBO;

import com.slipper.core.transaction.ReadTransactional;
import com.slipper.core.transaction.WriteTransactional;
import com.slipper.util.Tool;
import com.slipper.util.UUID;

@Service
@ReadTransactional
public class BackupService {
    @Resource
    private AffixItemService affixItemService;
    @Resource
    private BackupDao backupDao;

    public PageInfo<BackupBO> queryPage(BackupBO bo) {
        PageHelper.startPage(bo.getPageNumber(), bo.getPageSize());
        return new PageInfo<>(backupDao.queryList(bo));
    }
    
    @WriteTransactional
    public int save(BackupBO bo) {
        if (Tool.isNotBlank(bo.getId())) {
            return backupDao.update(bo);
        } else {
            bo.setId(UUID.getUUID());
            bo.setCreateTime(System.currentTimeMillis());
            return backupDao.insert(bo);
        }
    }
    
    @WriteTransactional
    public int delete(BackupBO bo) {
        BackupBO backup = backupDao.queryBy(bo.getId());
        if (backup != null) {
            affixItemService.delete(backup.getFileId());
        }
        return backupDao.delete(bo);
    }
}
